
package cartagows.wsframework.wssecurity.policy.model;

import java.util.ArrayList;
import java.util.Iterator;

import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

import org.apache.neethi.PolicyComponent;

import cartagows.wsframework.wssecurity.policy.SP11Constants;
import cartagows.wsframework.wssecurity.policy.SP12Constants;
import cartagows.wsframework.wssecurity.policy.SPConstants;

public class SignedEncryptedParts extends AbstractSecurityAssertion {

    private boolean body;   
    private boolean attachments;    
    private ArrayList headers = new ArrayList();   
    private boolean signedParts;
    
    public SignedEncryptedParts(boolean signedParts, int version) {
        this.signedParts = signedParts;
        setVersion(version);
    }

    public boolean isBody() {
        return body;
    }

    public void setBody(boolean body) {
        this.body = body;
    }
    
    public boolean isAttachments() {
        return attachments;
    }

    public void setAttachments(boolean attachments) {
        this.attachments = attachments;
    }

    public ArrayList getHeaders() {
        return this.headers;
    }

    public void addHeader(Header header) {
        this.headers.add(header);
    }

    public boolean isSignedParts() {
        return signedParts;
    }

    public QName getName() {
        if (signedParts) {
            if ( version == SPConstants.SP_V12) {
                return SP12Constants.SIGNED_PARTS;
            } else {
                return SP11Constants.SIGNED_PARTS;
            }           
        }
        
        if ( version == SPConstants.SP_V12) {
            return SP12Constants.ENCRYPTED_PARTS;
        } else {
            return SP11Constants.ENCRYPTED_PARTS;
        }
        
    }

    public PolicyComponent normalize() {
        return this;
    }

    public void serialize(XMLStreamWriter writer) throws XMLStreamException {
        String localName = getName().getLocalPart();
        String namespaceURI = getName().getNamespaceURI();

        String prefix = writer.getPrefix(namespaceURI);

        if (prefix == null) {
            prefix = getName().getPrefix();
            writer.setPrefix(prefix, namespaceURI);
        }
            
        // <sp:SignedParts> | <sp:EncryptedParts> 
        writer.writeStartElement(prefix, localName, namespaceURI);
        
        // xmlns:sp=".."
        writer.writeNamespace(prefix, namespaceURI);
        
        if (isBody()) {
            // <sp:Body />
            writer.writeStartElement(prefix, SPConstants.BODY, namespaceURI);
            writer.writeEndElement();
        }
        
        Header header;        
        for (Iterator iterator = headers.iterator(); iterator.hasNext();) {
            header = (Header) iterator.next();
            // <sp:Header Name=".." Namespace=".." />
            writer.writeStartElement(prefix, SPConstants.HEADER, namespaceURI);
            // Name attribute is optional
            if (header.getName() != null) {
                writer.writeAttribute("Name", header.getName());
            }
            writer.writeAttribute("Namespace", header.getNamespace());
            
            writer.writeEndElement();
        }
        
        if (isAttachments() && version == SPConstants.SP_V12) {
            // <sp:Attachments />
            writer.writeStartElement(prefix, SPConstants.ATTACHMENTS, namespaceURI);
            writer.writeEndElement();
        }
        
        // </sp:SignedParts> | </sp:EncryptedParts>
        writer.writeEndElement();
    }    
    
    
}
